﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;
using AudioCore.Common;

namespace AudioCore.DAO
{
    public class AccountDao : BaseDao
    {
        public AccountDao()
            : base("Account", "AccountID")
        {
            db = DatabaseFactory.CreateDatabase(Constant.DATABASE);
        }

        public int IsExistByUserName(string userName)
        {
            string sql = "SELECT AccountID FROM Account WHERE UPPER(AccountName)='{0}'";
            sql = string.Format(sql, userName.ToUpperInvariant());
            object objAccountID = db.ExecuteScalar(CommandType.Text, sql);
            if (objAccountID != null)
            {
                return Int32.Parse(objAccountID.ToString());
            }
            else
            {
                return -1;
            }
        }

        public int UpdatePasswd(int accountID, string newPasswd)
        {
            string sql = "UPDATE Account SET [Passwd] = {0} WHERE AccountID = '{1}'";
            sql = string.Format(sql, newPasswd, accountID);

            return db.ExecuteNonQuery(CommandType.Text, sql);
        }

        public DataTable GetAccountByUserName(string userName)
        {
            string sql = "SELECT * FROM Account WHERE UPPER(AccountName)= '{0}'";
            sql = string.Format(sql, userName);

            return db.ExecuteDataSet(CommandType.Text, sql).Tables[0];
        }

        public DataTable GetAccountByAccount(string userName)
        {
            string sql = "SELECT * FROM Account WHERE UPPER(AccountName)= '{0}'";
            sql = string.Format(sql, userName);

            return db.ExecuteDataSet(CommandType.Text, sql).Tables[0];
        }

        public DataTable GetAccountByAccountID(int accountID)
        {
            string sql = "SELECT * FROM Account WHERE AccountID = {0}";
            sql = string.Format(sql, accountID);

            return db.ExecuteDataSet(CommandType.Text, sql).Tables[0];
        }


    }
}
